home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 034a / twview82.zip / VIEWDOS.INC < prev   
Text File  |  1991-02-04  |  13KB  |  434 lines

  1.  
  2. procedure View;
  3. var
  4.   Grid      : screen;
  5.   OnScreen  : SectorToScreen;
  6.   XMax      : integer;
  7.   XDim      : XIndex;
  8.   XLength   : integer;
  9.   YMax      : integer;
  10.   YDim      : YIndex;
  11.   YLength   : integer;
  12.  
  13. function xpixel( i,j : integer ) : integer;
  14. begin
  15.   if not odd( j ) then
  16.     xpixel := (2 * i - 1) * XLength
  17.   else
  18.     xpixel := 2 * i * XLength;
  19. end;
  20.  
  21. function ypixel( i,j : integer ) : integer;
  22. begin
  23.   ypixel := (2 * j - 1) * Ylength;
  24. end;
  25.  
  26. function NumVal( n : integer ) : string;
  27. var
  28.   temp : string;
  29. begin
  30.   str( n, temp );
  31.   NumVal := temp;
  32. end; {NumVal}
  33.  
  34. procedure Tag( var STS : sectorToScreen;
  35.                var scr : screen;
  36.                    num : sector;
  37.                   irow : XIndex;
  38.                   jcol : YIndex );
  39. { put sector num into screen scr at irow, jcol; update sts accordingly }
  40. begin
  41.   if sts[ num].visible then
  42.     writeln('sector ', num, ' already placed before Tag!')
  43.   else if scr[ irow, jcol ].sectorNum <> 0 then
  44.     writeln('row ', irow, ', col ', jcol, ' already in use!')
  45.   else
  46.     begin
  47.       with STS[ num ] do
  48.         begin
  49.           visible := true;
  50.           row     := irow;
  51.           col     := jcol;
  52.         end; {with}
  53.       scr[ irow, jcol ].SectorNum := num;
  54.     end; {else}
  55. end; {tag}
  56.  
  57. procedure CheckOffspring( var P : Queue; where : sector; maxDist : integer);
  58. { Check all sectors from "where" to see if they should be pushed
  59. onto the Queue }
  60. var
  61.   t : warpIndex;
  62. begin
  63.   with space.sectors[ where ] do
  64.     if number > 0 then
  65.       for t := 1 to number do
  66.         if (not OnScreen[ data[ t ] ].visible) and
  67.            (Distances[ data[t] ].d <= maxDist)    then
  68.           enqueue( P, where, data[ t ] );
  69. end; {check offspring}
  70.  
  71. procedure GoDirection( d : integer;
  72.                    var Row   : XIndex;
  73.                    var Col   : YIndex);
  74. { 0 is upleft, 1 left, 2 downleft, 3 downright, etc mod 6 }
  75. begin
  76.   d := abs( d ) mod 6;
  77.   if odd( Col ) then
  78.     case d of
  79.       0 : begin
  80.             if Col > 1 then col := col - 1;
  81.             if Row < XDim then row := row + 1;
  82.           end;
  83.       1 : if Row < XDim then row := row + 1;
  84.       2 : begin
  85.             if Col < YDim then col := col + 1;
  86.             if Row < XDim then row := row + 1;
  87.           end;
  88.       3 : if Col < YDim then col := col + 1;
  89.       4 : if row > 1 then row := row - 1;
  90.       5 : if Col > 1 then col := col - 1;
  91.     end {case}
  92.   else
  93.     case d of
  94.     0 : if Col > 1 then col := col - 1;
  95.     1 : if Row < XDim then row := row + 1;
  96.     2 : if Col < YDim then col := col + 1;
  97.     3 : begin
  98.           if Col < YDim then col := col + 1;
  99.           if Row > 1 then row := row - 1;
  100.         end;
  101.     4 : if Row > 1 then row := row - 1;
  102.     5 : begin
  103.           if Col > 1 then col := col - 1;
  104.           if Row > 1 then row := row - 1;
  105.         end;
  106.     end; {case}
  107. end;
  108.  
  109. procedure seek( var freerow : Xindex; var freecol : Yindex; home : sector );
  110. const
  111.   MaxTries = 100;
  112. var
  113.   one, two, three, n : integer;
  114. { Trying to find a home for the new guy, close to the home sector.
  115. one, two, and three will be random directions to try (of radius 1, 2, and
  116. 3).  When we are successful, we just break out of the procedure, hopefully
  117. returning a freerow and freecol. }
  118. begin
  119.   one := random( 6 );
  120.   for one := one to one + 5 do { from random start, advance 5 positions }
  121.     begin
  122.       freerow := OnScreen[ home ].row;
  123.       freecol := OnScreen[ home ].col;
  124.       GoDirection( one, freerow, freecol );
  125.       if grid[ freerow, freecol ].SectorNum = 0 then
  126.         exit;
  127.     end; {one}
  128.   one := random( 6 );
  129.   two := random( 6 );
  130.   for one := one to one + 5 do
  131.     for two := two to two + 5 do
  132.       begin
  133.         freerow := OnScreen[ home ].row;
  134.         freecol := OnScreen[ home ].col;
  135.         GoDirection( one, freerow, freecol );
  136.         GoDirection( two, freerow, freecol );
  137.         if grid[ freerow, freecol ].SectorNum = 0 then
  138.           exit;
  139.       end; {one two}
  140.   one := random( 6 );
  141.   two := random( 6 );
  142.   three := random( 6 );
  143.   for one := one to one + 5 do
  144.     for two := two to two + 5 do
  145.       for three := three to three + 5 do
  146.         begin
  147.           freerow := OnScreen[ home ].row;
  148.           freecol := OnScreen[ home ].col;
  149.           GoDirection( one, freerow, freecol );
  150.           GoDirection( two, freerow, freecol );
  151.           GoDirection( three, freerow, freecol );
  152.           if grid[ freerow, freecol ].SectorNum = 0 then
  153.             exit;
  154.         end; {one two three}
  155.   writeln('couldn''t place anything near ', home );
  156.   n := 0;
  157.   repeat
  158.     freerow := random( xdim ) + 1;
  159.     freecol := random( ydim ) + 1;
  160.     n := n + 1;
  161.   until (n = MaxTries) or (grid[ freerow, freecol ].sectorNum = 0);
  162. end; {seek}
  163.  
  164. procedure FindHome( var Grid : screen;
  165.                     var Showing : SectorToScreen;
  166.                         home, near : sector );
  167. { This is an interesting bit: given the home sector, find an open slot
  168. in the Grid to place the near sector. }
  169. var
  170.   basedir : integer;
  171.   baserow : XIndex;
  172.   basecol : YIndex;
  173. begin
  174. {  writeln('Trying to find a home for ', near, ' close to ', home );
  175.   writeln('starting at ', showing[ home ].row, showing[ home ].col ); }
  176.   seek( baserow, basecol, home );
  177.   if grid[ baserow, basecol ].SectorNum <> 0 then
  178.     writeln('Seek Failed!')
  179.   else
  180.     Tag( Showing, Grid, near, baserow, basecol );
  181. {  writeln('chose ', baserow, ' ', basecol );
  182.   readln; }
  183. end;
  184.  
  185. procedure PlaceSectors( var Grid  : screen;
  186.                         var Showing : SectorToScreen;
  187.                         var maxDist : integer;
  188.                         var BaseSect : sector );
  189. var
  190.   PlaceMe : Queue;
  191.   daddy, sonny : sector;
  192. begin
  193.   Tag( showing, Grid, baseSect, XDim div 2, YDim div 2 ); { put first in center}
  194.   PlaceMe.front := 0;
  195.   CheckOffspring( PlaceMe, baseSect, maxdist );
  196.   While PlaceMe.front <> 0 do
  197.     begin
  198.       serve( PlaceMe, daddy, sonny );
  199.       if not showing[ sonny ].visible then
  200.         begin
  201.           FindHome( Grid, Showing, daddy, sonny );
  202.           if Showing[ sonny ].visible then             { if he didn't make it}
  203.             CheckOffspring( PlaceMe, sonny, maxDist ); { don't look for kids }
  204.         end;{if not showing}
  205.     end; {while}
  206. end; {while}
  207.  
  208. procedure InitSectorToScreen( var s : SectorToScreen );
  209. var
  210.   n : sector;
  211. begin
  212.   for n := 1 to 1000 do
  213.     s[ n ].visible := false;
  214. end;
  215.  
  216. procedure InitScreen( var s : Screen );
  217. var
  218.   r : XIndex;
  219.   c : YIndex;
  220. begin
  221.   for r := 1 to XDim do for c := 1 to YDim do
  222.     s[ r, c ].sectorNum := 0;
  223. end;
  224.  
  225. procedure FillGrid( var Grid : screen;
  226.                     var Showing : SectorToScreen;
  227.                     var Distances : distanceArray );
  228. { Choose a sector, and fill Distances with distance to that sector,
  229. as well as Showing and Grid based on nearby vertices. }
  230. var
  231.   maxD : integer;
  232.   sn   : sector;
  233.   ch   : char;
  234. begin
  235.   repeat
  236.     write('Starting at which sector? ');
  237.     readln( sn );
  238.     if space.sectors[ sn ].number = 0 then
  239.       writeln('You have never visited ', sn );
  240.   until space.sectors[ sn ].number > 0;
  241.   FixDistances( sn, Distances );
  242.   repeat
  243.     write( 'Max distance to include? ');
  244.     readln( maxD );
  245.     writeln( 'Total of ', CountDist( maxD), ' at distance at most ', MaxD );
  246.     write('Is this okay?  (y/n) ');
  247.     readln( ch );
  248.   until ch in ['Y','y'];
  249.   InitSectorToScreen( Showing );
  250.   InitScreen( Grid );
  251.   PlaceSectors( Grid, Showing, maxD, sn );
  252. end; {FillGrid}
  253.  
  254. function PortColor( g : stuff ) : word;
  255. begin
  256.   if GetMaxColor = 1 then
  257.     PortColor := 0
  258.   else
  259.     case g of
  260.       -1 : PortColor := 0;
  261.        0 : PortColor := 1;
  262.        1 : PortColor := 2;
  263.        2 : PortColor := 3;
  264.        3 : PortColor := 4;
  265.        4 : PortColor := 5;
  266.        5 : PortColor := 9;
  267.        6 : PortColor :=10;
  268.        7 : PortColor :=11;
  269.        8 : PortColor :=12;
  270.     end; {case}
  271. end; {PortColor}
  272.  
  273. function  SectorColor( s : sector ) : word;
  274. begin
  275.   if GetMaxColor = 1 then {monochrome}
  276.     SectorColor := 1
  277.   else  {not monochrome }
  278.     if space.sectors[ s ].number > 0 then
  279.       if space.sectors[ s ].porttype <> NotAPort then
  280.         SectorColor := GetMaxColor
  281.       else
  282.         SectorColor := 7
  283.     else
  284.       SectorColor := 14;
  285. end; {SectorColor}
  286.  
  287. procedure CircleSector( x : XIndex; y : YIndex; s : sector );
  288. var
  289.   r, c, xradius : integer;
  290.   ColorUsed,
  291.   xasp, yasp    : word;
  292. begin
  293.   r := xpixel( x, y );
  294.   c := ypixel( x, y );
  295.   GetAspectRatio( xasp, yasp );
  296.   xradius := round( yasp/xasp * ylength/2);
  297.   SetFillStyle( 0, 0);
  298.   SetLineStyle( SolidLn, 0, NormWidth );
  299.   if space.sectors[ s ].number > 0 then
  300.     SetColor( GetMaxColor )
  301.   else
  302.     SetColor( 0 );
  303.   FillEllipse( r, c, xradius, ylength div 2);
  304.   SetColor( SectorColor( s ) );
  305.   with space.sectors[ s ] do
  306.     if number <> Unexplored then
  307.       if porttype <> NotAPort then
  308.         begin
  309.           ColorUsed := PortColor( space.sectors[ s ].porttype );
  310.           SetFillStyle( 1, ColorUsed );
  311.           bar( r - xradius, c - ylength div 2, r + xradius,
  312.                c + ylength div 2 );
  313.           rectangle( r - xradius, c - ylength div 2,
  314.                r + xradius, c + ylength div 2 );
  315.           if ColorUsed > 9 then
  316.             SetColor( 0 );
  317.         end {if if}
  318.       else
  319.         circle( r, c, xradius );
  320.     outTextXY( r, c, NumVal( s ) );
  321. end;
  322.  
  323. procedure ConnectVertices( i1, i2 : XIndex; j1, j2 : YIndex;
  324.                            TwoWay : boolean );
  325. var
  326.   x1, y1, x2, y2 : integer;
  327. begin
  328.   x1 := xpixel( i1, j1 );
  329.   y1 := ypixel( i1, j1 );
  330.   x2 := xpixel( i2, j2 );
  331.   y2 := ypixel( i2, j2 );
  332.   if TwoWay then
  333.     SetLineStyle( SolidLn, 0, NormWidth )
  334.   else
  335.     SetLineStyle( DashedLn, 0, ThickWidth );
  336.   MoveTo( x1, y1 );
  337.   LineTo( x2, y2 );
  338. end;
  339.  
  340. procedure DrawGrid( var G : screen; STS : SectorToScreen );
  341. var
  342.   i : XIndex;
  343.   j : YIndex;
  344.   t : WarpIndex;
  345.   temp : integer;
  346. begin
  347.   for i := 1 to XDim do
  348.     for j := 1 to YDim do
  349.       if G[ i, j ].sectorNum <> 0 then
  350.         with G[ i, j ] do
  351.           with space.sectors[ sectorNum ] do if number > 0 then
  352.             for t := 1 to number do
  353.               if STS[ data[ t ] ].visible then
  354.                 ConnectVertices( i, STS[data[t] ].row, j, STS[data[t]].col,
  355.                                  IsWarp( data[t], sectorNum ) );
  356.   for i := 1 to XDim do
  357.     for j := 1 to YDim do
  358.       if G[ i, j ].sectorNum <> 0 then
  359.           CircleSector( i, j, G[i,j].sectorNum );
  360. end;
  361.  
  362. {$I initgrph.inc }
  363.  
  364. procedure GetDimensions( var x : XIndex; var xl : integer;
  365.                          var y : YIndex; var yl : integer );
  366. const
  367.   whitespace : set of char = [' ', #9, #10, #13 ];
  368. var
  369.   line : string;
  370.   ok   : boolean;
  371.   tempx, tempy,
  372.   position : integer;
  373. begin
  374.   ok := false;
  375.   repeat
  376.     write('Max dimensions? [', XDimMax, ' by ', YDimMax, ']  ');
  377.     readln( line );
  378.     if line = '' then
  379.       begin
  380.         ok := true;
  381.         x := XDimMax * 2 div 3;
  382.         y := YDimMax * 2 div 3;
  383.       end
  384.     else
  385.       begin
  386.         position := 1;
  387.         tempx := 0;
  388.         while (position <= length( line )) and
  389.               (line[position] in ['0'..'9']) do
  390.           begin
  391.             tempx := 10 * tempx + ord( line[ position ] ) - ord( '0' );
  392.             inc( position );
  393.           end; {while}
  394.         inc( position );
  395.         while (position <= length( line ) ) and
  396.               (line[position] in whitespace) do
  397.           inc( position );
  398.         tempy := 0;
  399.         while (position <= length( line )) and
  400.               (line[position] in ['0'..'9']) do
  401.           begin
  402.             tempy := 10 * tempy + ord( line[position] ) - ord('0');
  403.             inc( position );
  404.           end; {while}
  405.         ok := (tempx>0) and (tempx<=XDimMax) and (tempy>0) and (tempy<=YDimMax);
  406.         if ok then
  407.           begin
  408.             x := tempx;
  409.             y := tempy;
  410.           end {if}
  411.         else
  412.           begin
  413.             writeln('I don''t understand ', line );
  414.             writeln('Please give two integers separated by a space.');
  415.           end; {else}
  416.       end; {else}
  417.   until ok;
  418.   InitGraphics;
  419.   XMax := GetMaxX;
  420.   YMax := GetMaxY;
  421.   closeGraph;
  422.   xl := trunc( XMax / x / 2 );
  423.   yl := trunc( YMax / y / 2);
  424. end;
  425.  
  426. begin {view}
  427.     GetDimensions( XDim, XLength, YDim, Ylength );
  428.     FillGrid( Grid, OnScreen, Distances );
  429.     InitGraphics;
  430.     DrawGrid( Grid, Onscreen );
  431.     readln;
  432.     closeGraph;
  433. end; {view}
  434.